<?php
define('IN_PHPBB', true);
// Specify the path to you phpBB3 installation directory.
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
// The common.php file is required.
include($phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin(false);
$auth->acl($user->data);

$user->setup();

if (isset($config['molten_chat_installed']) and !empty($config['molten_chat_enable']))
{
	$color = false;
	$length = strlen ($user->data['user_colour']);

	if ($length == 6)
	{
		$color = array(
			'red' => hexdec($user->data['user_colour'][0].$user->data['user_colour'][1]),
			'green' => hexdec($user->data['user_colour'][2].$user->data['user_colour'][3]),
			'blue' => hexdec($user->data['user_colour'][4].$user->data['user_colour'][5])
		);
	}
	elseif ($length == 3)
	{
		$color = array(
			'red' => hexdec($user->data['user_colour'][0].$user->data['user_colour'][0]),
			'green' => hexdec($user->data['user_colour'][1].$user->data['user_colour'][1]),
			'blue' => hexdec($user->data['user_colour'][2].$user->data['user_colour'][2])
		);
	}

	$instance = request_var('instance', 'new');

	// Only registered users are handled server-side.
	if ($user->data['is_registered'] and $instance != 'new')
	{
		// Typecase $instance to an integer.
		$instance = (integer) $instance;

		// Update user's time.
		molten_chat_server::update($user->data['username'], false, false, $color);
	}
	elseif ($instance == 'new')
	{
		// Join the user to the room.
		$instance = molten_chat_server::join($user->data['username'], false, $user->session_id, $color);
	}

	if (isset ($_GET ['action']) and isset ($_GET ['level']) and isset ($_GET ['line']))
	{
		// Delay execution until new information is available.
		if (molten_chat_server::push((integer) $_GET['level'], (integer) $_GET['line'], (integer) $_GET['action'], array('identifier' => $user->session_id, 'instance' => $instance)))
		{
			// Get new information (if there is any).
			molten_chat_server::retrieve((integer) $_GET['level'], (integer) $_GET['line'], (integer) $_GET['action'], $instance);
		}
	}
	else
	{
		sleep(30);
	}
}
else
{
	sleep(30);
}